﻿using Microsoft.AspNetCore.Mvc;

namespace RESTfulDemo.Controllers {

    [Route("api/asyncs")]
    public class AsyncReturnDemo : ControllerBase {


        [HttpGet("one")]
        public async Task NoneReturn() {
            for (int i = 0; i < 10; i++) {
                await TaskTest();
            }
        }

        [HttpGet("two")]
        public async Task<string> ValueReturn() {
            return await Task.Run(() => "My name is UnitySir");
        }

        [HttpGet("three")]
        public async void VoidReturn() {
            for (int i = 0; i < 10; i++) {
                VoidTest();
            }
        }


        [HttpGet("four")]
        public async IAsyncEnumerable<string> GetNames() {
            await foreach (var item in Names()) {
                var data = item + $"_{DateTime.Now:yyyy:MM:dd HH:mm:ss}";
                await Console.Out.WriteLineAsync(data);
                if (item.Equals("UnitySir3")) {
                    yield break;
                }
                yield return data;
            }
        }

        async IAsyncEnumerable<string> Names() {
            for (int i = 0; i < 5; i++) {
                await Task.Delay(1000);
                yield return "UnitySir" + i;
            }
        }

        async void VoidTest() {
            await Console.Out.WriteLineAsync("Hello,UnitySir");
            await Task.Delay(1000);
        }

        async Task TaskTest() {
            await Console.Out.WriteLineAsync("Hello,UnitySir");
            await Task.Delay(1000);
        }
    }
}
